package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Integral;
import cn.wolfcode.trip.base.mapper.IntegralMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IIntegralService;
import cn.wolfcode.trip.base.service.IIntegralTypeService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：积分明细业务层接口实现类
 *
 * @package: cn.wolfcode.trip.base.service.impl
 * @author: Patch
 * @date: 2019/5/30 9:53
 * 总结:
 */
@Service
public class IntegralServiceImpl implements IIntegralService {
    @Autowired
    private IntegralMapper integralMapper;
    @Autowired
    private IUserService userService;
    @Autowired
    private IIntegralTypeService typeService;

    @Override
    public void saveOrUpdate(Integral integral) {
        // 获取对应类型的积分,并设置
        Integer grade = typeService.selectGradeById(integral.getType().getId());
        if (grade != null){
            // 添加商品为空
            integral.setGrade(grade);
        }
        // 1.设置更新时间
        integral.setInputTime(new Date());
        typeService.updateGrade(integral.getType().getId(), integral.getGrade());
            // 2.修改用户积分
            // 2.1判断是增加还是减少
            if (integral.getGrade() > 0) {
                // 2.2增加
                userService.addGrade(integral.getUser().getId(), integral.getGrade());
            } else {
                // 2.3减少
                userService.minusGrade(integral.getUser().getId(), integral.getGrade());
            }
            // 新增积分明细
            integralMapper.insert(integral);

    }

    @Override
    public PageInfo<Integral> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Integral> list = integralMapper.selectForList(qo);
        return new PageInfo<>(list);
    }



    @Override
    public void sign(Integral integral) {
        /**
         * 通过当前时间和用户的id和积分类型查询积分明细记录
         * 如果有数据表示已经签到，没有数据表示可以签到
         */
        // 1.查询二十四小时之前的日期
        Date oldTime = DateUtil.getTimeByHour(-24);
        Date currentTime = new Date();
        Integral integral1 = integralMapper.selectSign(integral.getUser().getId(), integral.getType().getId(), oldTime, currentTime);
        if (integral1 != null) {
            throw new RuntimeException("亲，您今天已经签到了");
        }
        // 2.签到，修改积分，添加明细,设置更新时间
        integral.setInputTime(new Date());
        // 获取对应类型的积分,并设置
        Integer grade = typeService.selectGradeById(integral.getType().getId());
        integral.setGrade(grade);
        integral.setRemark(integral.getUser().getNickName()+"用户已签到签到,获得："+integral.getGrade()+"积分");
        userService.addGrade(integral.getUser().getId(), grade);
        integralMapper.insert(integral);
    }

    @Override
    public List<Map<String,Object>> signDay(Integer month, Integral integral) {
        // 查询出来的用户当月签到数据
        List<Date> list = integralMapper.selectSignDay(month,integral.getUser().getId(), integral.getType().getId());
        // 创建日历类
        Calendar calendar = Calendar.getInstance();
        List<Map<String,Object>> days = new ArrayList<>();
        for (Date date : list) {
            Map<String, Object> map = new HashMap<>();
            //放入Date类型数据
            calendar.setTime(date);
            int day = calendar.get(Calendar.DATE);
            map.put("signDay", String.valueOf(day));
            days.add(map);
        }
        return days;
    }

    @Override
    public List<Integral> selectAll() {
        return integralMapper.selectAll();
    }

    @Override
    public Integer getCount() {
        return integralMapper.getCount();
    }

    @Override
    public List<Integral> roll(Integral integral) {
        return integralMapper.roll(integral);
    }
}
